<template>
  <!-- tabbar -->
  <div class="list">
    <basic-list @load="loadMore" :isFinished="isFinished" :isLoading="isLoading">
      <block v-if="currentTab === '景点门票'">
        <ticket-item  v-for="item in tickets" :key="item.id" :item="item" @click="handleClick(item)"/>
      </block>
      <block v-else>
        <trip-item v-for="item in trips" :key="item.productCode" :item="item" @click="handleClick(item)"/>
      </block>
    </basic-list>
  </div>
</template>
<script>
import sdk from 'sdk'
import ticketItem from '@/components/business/product-item/ticket-item'
import tripItem from '@/components/business/product-item/trip-item'
import sleep from '@/utils/helper/sleep'
import {encodeQuery} from 'utils/helper/query'
import { searchRecommendRoutes, searchRecommendTickets } from '@/api/index'

export default {
  name: 'index-list',
  components: {
    ticketItem,
    tripItem
  },
  props:{
    currentTab:{
      type: String,
      default: '景点门票'
    },
    params: {
      type: Object,
      default: {}
    }
  },
  watch: {
    currentTab(val) {
      this.isFinished = false
      this.loadMore()
    },
    params:{
				handler: function(val) {
          // console.log('首页列表组件params入参：', val)
            this.isFinished = false
            this.loadMore()
				},
        deep: true,
        immediate: true
		}
  },
  data() {
    return {
      tickets: [],
      trips: [],
      isFinished: false,
      isLoading: false,
      ticketParams: {
        pageNum: 1,
        pageSize: 10,
        regionId: 0
      },
      tripParams: {
        pageNum: 1,
        pageSize: 10,
        regionId: 0
      }
    }
  },
  async mounted() {
  },
  methods: {
    loadTickects() {
      if(!this.ticketParams.pageSize ||this.isFinished) return
        this.isLoading = true
        const {isCurrentCity, lat, lon} = this.params
        const ticketsParams = {
          ...this.ticketParams,
          regionId: this.params.regionId,
          lat: isCurrentCity ? lat: null,
          lon: isCurrentCity ? lon: null,
        }
        searchRecommendTickets(ticketsParams).then(res=>{
          this.isLoading = false
          this.isFinished = true
          this.tickets = res.result.ticketInfos
        })
    },
    loadTrips() {
      if(!this.tripParams.pageSize ||this.isFinished) return
        this.isLoading = true
        searchRecommendRoutes({ ...this.tripParams, regionId: this.params.regionId }).then(res=>{
          this.isLoading = false
          this.trips = res.result.routeInfos
          this.isFinished = true
        })
    },
    async loadMore() {
        if (this.isLoading || this.isFinished) return 
        if (this.currentTab === '景点门票') {
         await this.loadTickects()
        } else {
         await  this.loadTrips()
        }
    },

    /**
     * 点击列表项
     * @param {Object} item 列表项对象 
     */
    handleClick(item) {
      console.log('item:::', item)
      let path = ''
      let queryStr = encodeQuery({
        code: item.productCode
      })

      if(this.currentTab === '景点门票'){
        path = '/packageDetail/pages/tickets/detail/index'
        // path = '/ubd/product/poi/detail'
      } else {
        path = '/packageDetail/pages/tourline/detail/index'
        // path = '/ubd/product/tours/detail'
      }

      sdk.navigateTo({
        url: `${path}?${queryStr}`
      })
    }
  }
}
</script>
<style lang="less" scoped>
  
</style>